package cn.abcsys.cloud.devops.runtime.controller;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.abcsys.cloud.devops.runtime.bean.GridBean;
import cn.abcsys.cloud.devops.runtime.bean.Result;
import cn.abcsys.cloud.devops.runtime.entity.Image;
import cn.abcsys.cloud.devops.runtime.model.ImageModel;
import cn.abcsys.cloud.devops.runtime.service.ImageService;
import net.sf.json.JSONObject;


/**
 * 集群管理控制器
 * @author mayunhao
 *
 */
@RestController
@RequestMapping("/image")
public class ImageController {
	private static final Logger LOGGER = Logger.getLogger(ImageController.class);
	@Autowired
	private ImageService imageService;
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	@ResponseBody
	public GridBean imageList(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return null;
		}else{
			ImageModel imageModel = new ImageModel();
			JSONObject orig = JSONObject.fromObject(params);
			imageModel = (ImageModel)JSONObject.toBean(orig,ImageModel.class);
			return imageService.getImagePageList(imageModel);
		}
	}
	@RequestMapping(value = "/listAll", method = { RequestMethod.GET })
	@ResponseBody
	public Result listAll(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Image image = new Image();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				image = (Image)JSONObject.toBean(orig,Image.class);
			}catch(Exception e){
				return new Result(false, "参数转换异常："+e.getMessage());
			}
			return imageService.selectAllImageByEvn(image);
		}
	}
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	@ResponseBody
	public Result getDetail(@RequestParam(value = "imageId", required = true)Integer imageId){
		if(null==imageId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}
		return imageService.selectByPrimaryKey(imageId);
	}
	@RequestMapping(value = "/create", method = { RequestMethod.GET })
	@ResponseBody
	public Result create(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			return imageService.insertImage(params);
		}
	}
	@RequestMapping(value = "/modify", method = { RequestMethod.GET })
	@ResponseBody
	public Result modify(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Image image = new Image();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				image = (Image) JSONObject.toBean(orig,Image.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return new Result(false, "请求参数转换错误："+e.getMessage());
			}
			return imageService.updateImage(image);
		}
	}
	@RequestMapping(value = "/remove", method = { RequestMethod.GET })
	@ResponseBody
	public Result remove(@RequestParam(value = "imageId", required = true)Integer imageId){
		if(null==imageId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			return imageService.deleteImage(imageId);
		}
	}
	@RequestMapping(value = "/getUndeployImagesByProjectId", method = { RequestMethod.GET })
	@ResponseBody
	public Result getUndeployImagesByProjectId(@RequestParam(value = "projectId", required = true) Integer projectId){
		if(null==projectId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			return imageService.getUndeployImagesByProjectId(projectId);
		}
	}
	@RequestMapping(value = "/upload", method = { RequestMethod.GET })
	@ResponseBody
	public Result upload(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				com.alibaba.fastjson.JSONObject paramIn = com.alibaba.fastjson.JSONObject
						.parseObject(JSON.toJSONString(params));
				return imageService.uploadImage(paramIn);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
}
